package com.yuqing.myblogServe.service.impl;

import com.yuqing.myblogServe.dao.entity.ArticleCommentReply;
import com.yuqing.myblogServe.dao.entity.SysUser;
import com.yuqing.myblogServe.dao.mapper.ArticleCommentMapper;
import com.yuqing.myblogServe.dao.mapper.ArticleCommentReplyMapper;
import com.yuqing.myblogServe.dao.mapper.ArticleMapper;
import com.yuqing.myblogServe.dao.mapper.SysUserMapper;
import com.yuqing.myblogServe.dto.ArticleCommentReplyDto;
import com.yuqing.myblogServe.service.ArticleCommentReplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lyq
 * @date 2023/12/9
 **/

@Service
public class ArticleCommentReplyServiceImpl implements ArticleCommentReplyService {

    @Autowired
    ArticleCommentReplyMapper articleCommentReplyMapper;

    @Autowired
    ArticleCommentMapper articleCommentMapper;

    @Autowired
    SysUserMapper sysUserMapper;

    @Autowired
    ArticleMapper articleMapper;

    @Override
    public void replyComment(Long userId, ArticleCommentReply articleCommentReply) {

        articleCommentReply.setUserId(userId);
        articleCommentReply.setCreateTime(LocalDateTime.now());
        articleCommentReply.setUpdateTime(LocalDateTime.now());
        // 回复评论
        articleCommentReplyMapper.insert(articleCommentReply);
        // 回复评论数+1
        articleCommentMapper.addReplyCommentCount(articleCommentReply.getCommentId());

    }

    @Override
    public List<ArticleCommentReplyDto> getCommentReplyList( String commentId) {

        List<ArticleCommentReplyDto> list = articleCommentReplyMapper.listCommentReply(commentId);

        for(ArticleCommentReplyDto item: list ) {
            if(item.getReplyId() != null) {
                item.setToUserName(sysUserMapper.usernameById(item.getReplyId()));
            }
        }
        return list;
    }
}
